<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="app"></div>

    <script type="module">
      import { createApp } from "./lib/vue3.esm.browser.js";

      const app = createApp({
        template: `
          <h2>书籍列表</h2>
          <BookList />

          <h2>待办列表</h2>
          <TodoList />
        `,
      });

      app.component("BookList", {
        template: `
          <ul>
            <li v-for="item in list" :key="item.id">
              <div>书名：{{ item.title }}</div>  
            </li>  
          </ul>
        `,

        data() {
          return {
            list: [],
          };
        },

        methods: {
          // 刷新数据
          refresh() {
            fetch("http://localhost:3000/books")
              .then((response) => response.json())
              .then((result) => {
                console.log("resut", result);
                this.list = result;
              });
          },
        },

        created() {
          // 默认刷新一次
          this.refresh();
        },
      });

      app.component("TodoList", {
        template: `
          <ul>
            <li v-for="item in list" :key="item.id">
              <label>
                <input type="checkbox" />  
                <span>{{ item.name }}</span>
              </label>
            </li>
          </ul>
        `,

        data() {
          return {
            list: [],
          };
        },

        methods: {
          refresh() {
            fetch("http://localhost:3000/todos")
              .then((response) => response.json())
              .then((result) => {
                this.list = result;
              });
          },
        },

        created() {
          this.refresh();
        },
      });

      app.mount("#app");
    </script>
  </body>
</html>
